import rpg_component from "../../../component/rpg_component";
import type { Effect } from "../types";
import { effectKey, attackKey, Attribute_base } from "../../../shared/face/enums";
import BuffManager from "./BuffManager";
import { ComType } from "../../tool/component";
import BattleLogger from "../BattleLogger";

class EffectManage {
    private effectMap: Map<string, Effect> = new Map();
    
    constructor() {
        // 移除构造函数中的自动初始化，避免重复注册
    }
    
    init() {
        this.initDefaultEffects();
    }
    
    // 通用：固定值+属性值叠加
    private calcFixedAndAttribute(attacker: rpg_component, attKey: string, attRange: number, fixed: number, stackCount: number) {
        const fixedPart = fixed > 0 ? fixed * stackCount : 0;
        const base = attacker.getAttribute(attKey) || 0;
        const attrPart = Math.floor(base * attRange * stackCount);
        return fixedPart + attrPart;
    }
    
    private initDefaultEffects() {
        // 伤害类效果
        this.registerEffect(effectKey.伤害类_普通攻击, this.createDamageEffect(attackKey.普通攻击));
        this.registerEffect(effectKey.伤害类_物理伤害, this.createDamageEffect(attackKey.物理攻击));
        this.registerEffect(effectKey.伤害类_魔法伤害, this.createDamageEffect(attackKey.魔法攻击));
        this.registerEffect(effectKey.伤害类_真实伤害, this.createDamageEffect(attackKey.真实攻击));
        
        // 辅助类效果
        this.registerEffect(effectKey.辅助类_生命回复, this.createHealEffect());
        this.registerEffect(effectKey.辅助类_护盾, this.createShieldEffect());
        this.registerEffect(effectKey.辅助类_属性提升, this.createAttributeBoostEffect());
        
        // 工具类效果
        this.registerEffect(effectKey.工具类_添加buff, this.createAddBuffEffect());
        
    }
    
    // 创建伤害效果
    private createDamageEffect(attackType: attackKey): Effect {
        return (attacker: rpg_component, target: rpg_component, self: any, stackCount: number, index: number, SkillData: any) => {
            const attKey = SkillData.attKey || attackType;
            const attRange = SkillData.attRange || 1.0;
            const fixedDamage = SkillData.fixed || 0;
            
            // 通用叠加
            const finalDamage = this.calcFixedAndAttribute(attacker, attKey, attRange, fixedDamage, stackCount);
            
            if (finalDamage > 0) {
                // 调用hit方法处理伤害
                const actualDamage = target.hit(finalDamage, 0, attackType);
                
                // 记录伤害统计
                attacker.setLog(attacker, target, attackType, actualDamage, self);
                
                // 获取单位名称
                const attackerName = attacker.getComponent(ComType.base)?.nickName || attacker.id;
                const targetName = target.getComponent(ComType.base)?.nickName || target.id;
                
                // 移除战斗日志输出
            }else{
                BattleLogger.log('info',`${self.name} 没有伤害`);
            }
        };
    }
    
    // 创建治疗效果
    private createHealEffect(): Effect {
        return (attacker: rpg_component, target: rpg_component, self: any, stackCount: number, index: number, SkillData: any) => {
            const attKey = SkillData.attKey || attackKey.魔法攻击;
            const attRange = SkillData.attRange || 1.0;
            
            const baseHeal = attacker.getAttribute(attKey) || 0;
            const finalHeal = Math.floor(baseHeal * attRange * stackCount);
            
            if (finalHeal > 0) {
                target.heal(finalHeal);
                
                // 记录治疗统计
                attacker.setLog(attacker, target, attackKey.恢复生命, finalHeal, self);
                
                // 获取单位名称
                const attackerName = attacker.getComponent(ComType.base)?.nickName || attacker.id;
                const targetName = target.getComponent(ComType.base)?.nickName || target.id;
                
                // 移除治疗日志输出
            }
        };
    }
    
    // 创建护盾效果
    private createShieldEffect(): Effect {
        return (attacker: rpg_component, target: rpg_component, self: any, stackCount: number, index: number, SkillData: any) => {
            const attKey = SkillData.attKey || attackKey.魔法攻击;
            const attRange = SkillData.attRange || 1.0;
            const fixedShield = SkillData.fixed || 0;
            
            // 通用叠加
            const finalShield = this.calcFixedAndAttribute(attacker, attKey, attRange, fixedShield, stackCount);
            
            if (finalShield > 0) {
                target.addShield(finalShield);
                
                // 获取单位名称
                const attackerName = attacker.getComponent(ComType.base)?.nickName || attacker.id;
                const targetName = target.getComponent(ComType.base)?.nickName || target.id;
                
                // 移除护盾日志输出
            }
        };
    }
    
    // 创建属性提升效果
    private createAttributeBoostEffect(): Effect {
        return (attacker: rpg_component, target: rpg_component, self: any, stackCount: number, index: number, SkillData: any) => {
            const attKey = SkillData.attKey || attackKey.魔法攻击;
            const attRange = SkillData.attRange || 1.0;
            const attributeName = SkillData.attributeName || Attribute_base.物理攻击;
            const fixedBoost = SkillData.fixed || 0;
            
            // 通用叠加
            const finalBoost = this.calcFixedAndAttribute(attacker, attKey, attRange, fixedBoost, stackCount);
            
            if (finalBoost > 0) {
                const currentValue = target.getAttribute(attributeName) || 0;
                target.setAttribute(attributeName, currentValue + finalBoost);
                
                // 获取单位名称
                const attackerName = attacker.getComponent(ComType.base)?.nickName || attacker.id;
                const targetName = target.getComponent(ComType.base)?.nickName || target.id;
                
                // 移除属性提升日志输出
            }
        };
    }
    
    // 创建添加buff效果
    private createAddBuffEffect(): Effect {
        return (attacker: rpg_component, target: rpg_component, self: any, stackCount: number, index: number, SkillData: any) => {
            const buffName = SkillData.addBuffName;
            if (buffName) {
                BuffManager.applyBuff(target, buffName, attacker, SkillData);
                
                // 获取单位名称
                const attackerName = attacker.getComponent(ComType.base)?.nickName || attacker.id;
                const targetName = target.getComponent(ComType.base)?.nickName || target.id;
                
                // 移除buff添加日志输出
            }
        };
    }
    

    

    
    getEffect(key: string): Effect {
        let eff = this.effectMap.get(key) as Effect;
        if (!eff) {
            console.error(`效果不存在: ${key}`);
            // 返回一个空效果而不是退出程序
            return (attacker: rpg_component, target: rpg_component, self: any, stackCount: number, index: number, SkillData: any) => {
                console.warn(`未实现的效果: ${key}`);
            };
        }
        return eff;
    }
    
    registerEffect(key: string, effect: Effect) {
        console.log('注册效果:', key);
        this.effectMap.set(key, effect);
    }
}

export default new EffectManage();